package seap.actions;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import seap.beans.UsuarioBean;
import seap.dao.UsuarioDAO;

public class LoginAction extends DispatchAction {
	
	UsuarioDAO dao = new UsuarioDAO();
	
    private Log logger = LogFactory.getLog(this.getClass());
	
	public ActionForward efetuarLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	logger.debug("Login");
    	
    	String user = (String) request.getParameter("username");
    	String pass = (String) request.getParameter("pass");
    	
    	UsuarioBean u = null;
    	String r = "falha";
    	List<UsuarioBean> list = dao.localizarTodos(UsuarioBean.class);
    	
        Iterator iter = list.iterator();
        
        while(iter.hasNext()){
        	u = (UsuarioBean) iter.next();
        	if(user.equalsIgnoreCase(u.getUsuario())){
        		if(pass.equals(u.getSenha()))
        			r = "sucesso";
        	}
        }
    	return mapping.findForward(r);
    }
	
	public ActionForward efetuarLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	logger.debug("Logout");
 
    	return mapping.findForward("falha");
    }
}
